/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;



import java.awt.AlphaComposite;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Daniel
 */
public class TextureLoad {

    public static ArrayList<BufferedImage> decode(File in, int bw, int bh) {

        BufferedImage all = loadFullFile(in);
        ArrayList<BufferedImage> out = new ArrayList();

        BufferedImage blank = loadFullFile(new File("img/blank.png"));
        
        BufferedImage aux;
        
        /*-3179861 és el sRGB pel fons de les textures (aquell rosa mig-apagat)*/
        
        boolean isBlank = true;
        
        for (int i = 1; i + bh < all.getHeight(); i += bh + 2) {
            for (int j = 1; j + bw < all.getWidth(); j += bw + 2) {
                try {
                    aux = (BufferedImage) all.getSubimage(j, i, 20, 20);
                    
                    isBlank = true;
                    
                    for(int k = 0; k < aux.getHeight() && isBlank; k++){
                        for(int l = 0; l < aux.getWidth() && isBlank; l++){
                            if(!(aux.getRGB(l, k) == -3179861)){
                                
                                out.add(aux);
                                isBlank = false;
                            }
                            
//                            System.out.println(aux.getAlphaRaster());
                        }
                    }  
                    
                    
                    
                    
                } catch (Exception e) {
                    System.err.println("Texture not got (" + i + "," + 1 + ");\t");
                }
            }
        }

       
        return out;
    }

    public static BufferedImage loadFullFile(File in) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(in);
        } catch (IOException ex) {
            Logger.getLogger(TextureLoad.class.getName()).log(Level.SEVERE, null, ex);
        }
        return img;
    }

}
